<template>
  <el-dialog
    :title="!dataForm.id ? '新增楼层' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="房间楼层" prop="roomfloor">
      <el-input v-model="dataForm.roomfloor" placeholder="房间楼层"></el-input>
    </el-form-item>
    <el-form-item label="房间号" prop="roomno">
      <el-input v-model="dataForm.roomno" placeholder="房间号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          roomfloor: '',
          roomno: '',
          createtime: '',
          createby: '',
          updatetime: '',
          updateby: '',
          isdelete: ''
        },
        dataRule: {
          roomfloor: [
            { required: true, message: '房间楼层不能为空', trigger: 'blur' }
          ],
          roomno: [
            { required: true, message: '房间号不能为空', trigger: 'blur' }
          ],
          createtime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          createby: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          updatetime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          updateby: [
            { required: true, message: '更新者不能为空', trigger: 'blur' }
          ],
          isdelete: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/room/room/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.roomfloor = data.room.roomfloor
                this.dataForm.roomno = data.room.roomno
                this.dataForm.createtime = data.room.createtime
                this.dataForm.createby = data.room.createby
                this.dataForm.updatetime = data.room.updatetime
                this.dataForm.updateby = data.room.updateby
                this.dataForm.isdelete = data.room.isdelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/room/room/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'roomfloor': this.dataForm.roomfloor,
                'roomno': this.dataForm.roomno,
                'createtime': this.dataForm.createtime,
                'createby': this.dataForm.createby,
                'updatetime': this.dataForm.updatetime,
                'updateby': this.dataForm.updateby,
                'isdelete': this.dataForm.isdelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
